package com.zkzx.freight.bill.service;

import com.zkzx.common.freight.command.OrderCommand;
import com.zkzx.common.freight.model.OrderPayableVO;
import com.zkzx.freight.bill.mapper.OrderPayableMapper;
import com.zkzx.freight.bill.mapper.entity.OrderPayable;
import com.zkzx.freight.bill.mapper.entity.OrderPayableExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/12/18:14
 * @Description:
 */

@Slf4j
@Service
public class OrderPayableService {

    @Resource
    private OrderPayableMapper orderPayableMapper;

    /**
     * 根据订单id查支付信息
     * @param orderId 订单id
     * @return 支付信息
     */
    public OrderPayableVO findVOByOrderIdId(Integer orderId){
        OrderPayable orderPayable = findByOrderId(orderId);
        if (Objects.isNull(orderPayable)){
            return null;
        }
        return OrderPayableVO.builder()
                .id(orderPayable.getId())
                .orderId(orderPayable.getOrderid())
                .fare(orderPayable.getFare())
                .keepFareproPortion(orderPayable.getKeepfareproportion())
                .keepFare(orderPayable.getKeepfare())
                .totalCost(orderPayable.getTotalcost())
                .build();
    }

    /**
     * 根据订单id查支付信息
     * @param orderId 订单id
     * @return 支付信息
     */
    public OrderPayable findByOrderId(Integer orderId){
        OrderPayableExample orderPayableExample = new OrderPayableExample();
        orderPayableExample.createCriteria().andOrderidEqualTo(orderId);
        Optional<OrderPayable> orderPayableOne = orderPayableMapper.selectByExample(orderPayableExample).stream().findFirst();

        OrderPayable orderPayable=null;
        if (orderPayableOne.isPresent()){
            orderPayable=orderPayableOne.get();
        }
        return orderPayable;
    }

    /**
     * 插入支付信息
     * @param command 入参
     * @param id 订单id
     */
    public void insert(OrderCommand command, Integer id){
        if(Objects.isNull(command.getOrderPayableVO())){
            return;
        }
        OrderPayable orderPayable = new OrderPayable();
        orderPayable.setOrderid(id);
        Optional.ofNullable(command.getOrderPayableVO()).ifPresent(op->{
           orderPayable.setFare(command.getOrderPayableVO().getFare());
           orderPayable.setKeepfareproportion(command.getOrderPayableVO().getKeepFareproPortion());
           orderPayable.setKeepfare(command.getOrderPayableVO().getKeepFare());
           orderPayable.setTotalcost(command.getOrderPayableVO().getTotalCost());
        });
        orderPayableMapper.insertSelective(orderPayable);
    }


    /**
     * 更新支付信息
     * @param command 入参
     * @param id 订单id
     */
    public void update(OrderCommand command, Integer id){
        if(Objects.isNull(command.getOrderPayableVO())){
            return;
        }

        OrderPayable orderPayable = new OrderPayable();

        orderPayable.setOrderid(id);
        orderPayable.setFare(command.getOrderPayableVO().getFare());
        orderPayable.setKeepfareproportion(command.getOrderPayableVO().getKeepFareproPortion());
        orderPayable.setKeepfare(command.getOrderPayableVO().getKeepFare());
        orderPayable.setTotalcost(command.getOrderPayableVO().getTotalCost());

        OrderPayableExample orderPayableExample = new OrderPayableExample();
        OrderPayableExample.Criteria criteria = orderPayableExample.createCriteria();
        criteria.andOrderidEqualTo(id);
        orderPayableMapper.updateByExampleSelective(orderPayable,orderPayableExample);


    }

}
